<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - threaded_object_extension_abstract.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2007  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#undef</font> DLIB_THREADED_OBJECT_EXTENSIOn_ABSTRACT_
<font color='#0000FF'>#ifdef</font> DLIB_THREADED_OBJECT_EXTENSIOn_ABSTRACT_ 

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='threads_kernel_abstract.h.html'>threads_kernel_abstract.h</a>"

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>class</font> <b><a name='threaded_object'></a>threaded_object</b>
    <b>{</b>
        <font color='#009900'>/*!
            INITIAL VALUE
                - is_running() == false
                - is_alive() == false 
                - should_respawn() == false

            WHAT THIS OBJECT REPRESENTS
                This object represents a simple threaded object.  To use it you inherit
                from it and define the thread() function.  Then when you call start()
                it will spawn a thread that calls this-&gt;thread().  
        !*/</font>
    <font color='#0000FF'>public</font>:

        <b><a name='threaded_object'></a>threaded_object</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>;
        <font color='#009900'>/*!
            ensures
                - #*this is properly initialized
            throws
                - std::bad_alloc
                - dlib::thread_error
                    the constructor may throw this exception if there is a problem 
                    gathering resources to create threading objects.
        !*/</font>

        <font color='#0000FF'>virtual</font> ~<b><a name='threaded_object'></a>threaded_object</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>;
        <font color='#009900'>/*!
            requires
                - is_alive() == false
                  (i.e. in the destructor for the object you derive from this one you
                  must wait for this-&gt;thread() to end.)
            ensures
                - all resources allocated by *this have been freed.  
        !*/</font>

        <font color='#0000FF'><u>bool</u></font> <b><a name='is_running'></a>is_running</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
        <font color='#009900'>/*!
            requires
                - is not called from this-&gt;thread()
            ensures
                - if (is_alive() &amp;&amp; this-&gt;thread() is currently supposed to be executing) then
                    - returns true
                - else
                    - returns false
        !*/</font>

        <font color='#0000FF'><u>bool</u></font> <b><a name='is_alive'></a>is_alive</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
        <font color='#009900'>/*!
            requires
                - is not called from this-&gt;thread()
            ensures
                - if (this-&gt;thread() has been called by some thread and has yet to terminate) then
                    - returns true
                - else
                    - returns false
        !*/</font>

        <font color='#0000FF'><u>void</u></font> <b><a name='wait'></a>wait</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
        <font color='#009900'>/*!
            requires
                - is not called from this-&gt;thread()
            ensures
                - if (is_alive() == true) then
                    - blocks until this-&gt;thread() terminates
        !*/</font>

        <font color='#0000FF'><u>void</u></font> <b><a name='start'></a>start</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>;
        <font color='#009900'>/*!
            requires
                - is not called from this-&gt;thread()
            ensures
                - #is_alive() == true
                - #is_running() == true
                - #should_stop() == false
            throws
                - std::bad_alloc or dlib::thread_error
                    If either of these exceptions are thrown then 
                    #is_alive() == false and #is_running() == false
        !*/</font>

        <font color='#0000FF'><u>void</u></font> <b><a name='set_respawn'></a>set_respawn</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>;
        <font color='#009900'>/*!
            requires
                - is not called from this-&gt;thread()
            ensures
                - #should_respawn() == true
        !*/</font>

        <font color='#0000FF'><u>bool</u></font> <b><a name='should_respawn'></a>should_respawn</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
        <font color='#009900'>/*!
            requires
                - is not called from this-&gt;thread()
            ensures
                - returns true if the thread will automatically restart upon termination and
                  false otherwise.  Note that every time a thread starts it sets should_respawn() 
                  back to false.  Therefore, a single call to set_respawn() can cause at most
                  one respawn to occur. 
        !*/</font>

        <font color='#0000FF'><u>void</u></font> <b><a name='restart'></a>restart</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>;
        <font color='#009900'>/*!
            requires
                - is not called from this-&gt;thread()
            ensures
                - This function atomically executes set_respawn() and start().  The precise meaning of this
                  is defined below.
                - if (is_alive()) then
                    - #should_respawn() == true
                - else
                    - #should_respawn() == false 
                - #is_alive() == true
                - #is_running() == true
                - #should_stop() == false
            throws
                - std::bad_alloc or dlib::thread_error
                    If either of these exceptions are thrown then 
                    #is_alive() == false and #is_running() == false
        !*/</font>

        <font color='#0000FF'><u>void</u></font> <b><a name='pause'></a>pause</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>;
        <font color='#009900'>/*!
            requires
                - is not called from this-&gt;thread()
            ensures
                - #is_running() == false
        !*/</font>

        <font color='#0000FF'><u>void</u></font> <b><a name='stop'></a>stop</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>;
        <font color='#009900'>/*!
            requires
                - is not called from this-&gt;thread()
            ensures
                - #should_stop() == true
                - #is_running() == false
                - #should_respawn() == false
        !*/</font>

    <font color='#0000FF'>protected</font>:

        <font color='#0000FF'><u>bool</u></font> <b><a name='should_stop'></a>should_stop</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
        <font color='#009900'>/*!
            requires
                - is only called from the thread that executes this-&gt;thread()
            ensures
                - calls to this function block until (#is_running() == true || #should_stop() == true) 
                - if (this thread is supposed to terminate) then
                    - returns true
                - else
                    - returns false
        !*/</font>

    <font color='#0000FF'>private</font>:

        <font color='#0000FF'>virtual</font> <font color='#0000FF'><u>void</u></font> <b><a name='thread'></a>thread</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#009900'>/*!
            requires
                - is executed in its own thread
                - is only executed in one thread at a time
            throws
                - does not throw any exceptions
        !*/</font>

        <font color='#009900'>// restricted functions
</font>        <b><a name='threaded_object'></a>threaded_object</b><font face='Lucida Console'>(</font>threaded_object<font color='#5555FF'>&amp;</font><font face='Lucida Console'>)</font>;        <font color='#009900'>// copy constructor
</font>        threaded_object<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font><font face='Lucida Console'>(</font>threaded_object<font color='#5555FF'>&amp;</font><font face='Lucida Console'>)</font>;    <font color='#009900'>// assignment operator
</font>    <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_THREADED_OBJECT_EXTENSIOn_ABSTRACT_
</font>

</pre></body></html>